package org.jeecg.modules.wmhb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.UserConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.modules.wmhb.entity.WmTask;
import org.jeecg.modules.wmhb.entity.WmTaskFlow;
import org.jeecg.modules.wmhb.entity.WmTaskLog;
import org.jeecg.modules.wmhb.entity.WmTaskbook;
import org.jeecg.modules.wmhb.mapper.WmTaskLogMapper;
import org.jeecg.modules.wmhb.service.IWmTaskLogService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 任务日志
 * @Author: jeecg-boot
 * @Date:   2024-12-17
 * @Version: V1.0
 */
@Service
public class WmTaskLogServiceImpl extends ServiceImpl<WmTaskLogMapper, WmTaskLog> implements IWmTaskLogService {
    @Override
    @Transactional
    public List<WmTaskLog> initTaskLog(WmTask wmTask){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<WmTaskLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(WmTaskLog::getWmTaskId, wmTask.getId());
        List<WmTaskLog> wmTaskLogListExist = list(queryWrapper);

        if(wmTaskLogListExist.isEmpty()) {
            List<WmTaskLog> wmTaskLogList = new ArrayList<>();
            //1、创建任务
            WmTaskLog wmTaskLog1 = new WmTaskLog();
            wmTaskLog1.setWmTaskId(wmTask.getId());
            wmTaskLog1.setContent("创建任务");
            wmTaskLog1.setOperateBy(sysUser.getId());
            wmTaskLog1.setOperateTime(new Date());
            wmTaskLog1.setOperateType(UserConstant.TASK_LOG_TYPE_CREATE);
            wmTaskLog1.setIsOver(UserConstant.SYS_YES);
            wmTaskLog1.setOrderNum(1);
            wmTaskLogList.add(wmTaskLog1);

            //2、确认任务
            WmTaskLog wmTaskLog2 = new WmTaskLog();
            wmTaskLog2.setWmTaskId(wmTask.getId());
            wmTaskLog2.setContent("确认任务");
            wmTaskLog2.setOperateType(UserConstant.TASK_LOG_TYPE_CONFIRM);
            wmTaskLog2.setIsOver(UserConstant.SYS_NO);
            wmTaskLog2.setOrderNum(2);
            wmTaskLogList.add(wmTaskLog2);

            //3、审核任务
            WmTaskLog wmTaskLog3 = new WmTaskLog();
            wmTaskLog3.setWmTaskId(wmTask.getId());
            wmTaskLog3.setContent("审核任务");
            wmTaskLog3.setOperateType(UserConstant.TASK_LOG_TYPE_APPROVE);
            wmTaskLog3.setIsOver(UserConstant.SYS_NO);
            wmTaskLog3.setOrderNum(3);
            wmTaskLogList.add(wmTaskLog3);

            if (wmTask.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PRODUCE) || wmTask.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PRODUCE_ELECTRIC)) {
                //电气生产任务包含设计，机械为任务书之前已经设计好
                if(wmTask.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PRODUCE_ELECTRIC)) {
                    //4、设计任务
                    WmTaskLog wmTaskLog4 = new WmTaskLog();
                    wmTaskLog4.setWmTaskId(wmTask.getId());
                    wmTaskLog4.setContent("设计任务");
                    wmTaskLog1.setOperateBy(sysUser.getId());
                    wmTaskLog1.setOperateTime(new Date());
                    wmTaskLog4.setOperateType(UserConstant.TASK_LOG_TYPE_DESIGN);
                    wmTaskLog4.setIsOver(UserConstant.SYS_NO);
                    wmTaskLog4.setOrderNum(4);
                    wmTaskLogList.add(wmTaskLog4);
                }

                //5、任务报料
                WmTaskLog wmTaskLog5 = new WmTaskLog();
                wmTaskLog5.setWmTaskId(wmTask.getId());
                wmTaskLog5.setContent("任务报料");
                wmTaskLog5.setOperateType(UserConstant.TASK_LOG_TYPE_BOM_ORDER);
                wmTaskLog5.setIsOver(UserConstant.SYS_NO);
                wmTaskLog5.setOrderNum(5);
                wmTaskLogList.add(wmTaskLog5);

                //6、物料采购
                WmTaskLog wmTaskLog6 = new WmTaskLog();
                wmTaskLog6.setWmTaskId(wmTask.getId());
                wmTaskLog6.setContent("物料采购");
                wmTaskLog6.setOperateType(UserConstant.TASK_LOG_TYPE_BOM_PURCHASE);
                wmTaskLog6.setIsOver(UserConstant.SYS_NO);
                wmTaskLog6.setOrderNum(6);
                wmTaskLogList.add(wmTaskLog6);

                //7、物料到货
                WmTaskLog wmTaskLog7 = new WmTaskLog();
                wmTaskLog7.setWmTaskId(wmTask.getId());
                wmTaskLog7.setContent("物料到货");
                wmTaskLog7.setOperateType(UserConstant.TASK_LOG_TYPE_BOM_REACHED);
                wmTaskLog7.setIsOver(UserConstant.SYS_NO);
                wmTaskLog7.setOrderNum(7);
                wmTaskLogList.add(wmTaskLog7);

                //8、物料领取
                WmTaskLog wmTaskLog8 = new WmTaskLog();
                wmTaskLog8.setWmTaskId(wmTask.getId());
                wmTaskLog8.setContent("物料领取");
                wmTaskLog8.setOperateType(UserConstant.TASK_LOG_TYPE_CONSUME);
                wmTaskLog8.setIsOver(UserConstant.SYS_NO);
                wmTaskLog8.setOrderNum(8);
                wmTaskLogList.add(wmTaskLog8);

                //9、生产入库
                WmTaskLog wmTaskLog9 = new WmTaskLog();
                wmTaskLog9.setWmTaskId(wmTask.getId());
                wmTaskLog9.setContent("生产入库");
                wmTaskLog9.setOperateType(UserConstant.TASK_LOG_TYPE_PRODUCE);
                wmTaskLog9.setIsOver(UserConstant.SYS_NO);
                wmTaskLog9.setOrderNum(9);
                wmTaskLogList.add(wmTaskLog9);

                //10、仓库发货
                WmTaskLog wmTaskLog10 = new WmTaskLog();
                wmTaskLog10.setWmTaskId(wmTask.getId());
                wmTaskLog10.setContent("仓库发货");
                wmTaskLog10.setOperateType(UserConstant.TASK_LOG_TYPE_SEND);
                wmTaskLog10.setIsOver(UserConstant.SYS_NO);
                wmTaskLog10.setOrderNum(10);
                wmTaskLogList.add(wmTaskLog10);

                //11、产品到货
                WmTaskLog wmTaskLog11 = new WmTaskLog();
                wmTaskLog11.setWmTaskId(wmTask.getId());
                wmTaskLog11.setContent("产品到货");
                wmTaskLog11.setOperateType(UserConstant.TASK_LOG_TYPE_ARRIVE);
                wmTaskLog11.setIsOver(UserConstant.SYS_NO);
                wmTaskLog11.setOrderNum(11);
                wmTaskLogList.add(wmTaskLog11);

                //12、安装调试
                WmTaskLog wmTaskLog12 = new WmTaskLog();
                wmTaskLog12.setWmTaskId(wmTask.getId());
                wmTaskLog12.setContent("安装调试");
                wmTaskLog12.setOperateType(UserConstant.TASK_LOG_TYPE_DEBUG);
                wmTaskLog12.setIsOver(UserConstant.SYS_NO);
                wmTaskLog12.setOrderNum(12);
                wmTaskLogList.add(wmTaskLog12);
            }
            else if (wmTask.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_DESIGN)) {
                //4、设计任务
                WmTaskLog wmTaskLog4 = new WmTaskLog();
                wmTaskLog4.setWmTaskId(wmTask.getId());
                wmTaskLog4.setContent("设计任务");
                wmTaskLog4.setOperateType(UserConstant.TASK_LOG_TYPE_DESIGN);
                wmTaskLog4.setIsOver(UserConstant.SYS_NO);
                wmTaskLog4.setOrderNum(4);
                wmTaskLogList.add(wmTaskLog4);
                //5、工艺任务
                WmTaskLog wmTaskLog5 = new WmTaskLog();
                wmTaskLog5.setWmTaskId(wmTask.getId());
                wmTaskLog5.setContent("工艺任务");
                wmTaskLog5.setOperateType(UserConstant.TASK_LOG_TYPE_PROCESS);
                wmTaskLog5.setIsOver(UserConstant.SYS_NO);
                wmTaskLog5.setOrderNum(5);
                wmTaskLogList.add(wmTaskLog5);
            }
            else if (wmTask.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PROCESS)) {
                //4、工艺任务
                WmTaskLog wmTaskLog4 = new WmTaskLog();
                wmTaskLog4.setWmTaskId(wmTask.getId());
                wmTaskLog4.setContent("工艺任务");
                wmTaskLog4.setOperateType(UserConstant.TASK_LOG_TYPE_PROCESS);
                wmTaskLog4.setIsOver(UserConstant.SYS_NO);
                wmTaskLog4.setOrderNum(4);
                wmTaskLogList.add(wmTaskLog4);
            }
            else if (wmTask.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_TECHNOLOGY)) {
                //4、设计任务
                WmTaskLog wmTaskLog4 = new WmTaskLog();
                wmTaskLog4.setWmTaskId(wmTask.getId());
                wmTaskLog4.setContent("技术服务报料");
                wmTaskLog4.setOperateType(UserConstant.TASK_LOG_TYPE_DESIGN);
                wmTaskLog4.setIsOver(UserConstant.SYS_NO);
                wmTaskLog4.setOrderNum(4);
                wmTaskLogList.add(wmTaskLog4);
                //5、调试任务
                WmTaskLog wmTaskLog5 = new WmTaskLog();
                wmTaskLog5.setWmTaskId(wmTask.getId());
                wmTaskLog5.setContent("调试任务");
                wmTaskLog5.setOperateType(UserConstant.TASK_LOG_TYPE_DEBUG);
                wmTaskLog5.setIsOver(UserConstant.SYS_NO);
                wmTaskLog5.setOrderNum(5);
                wmTaskLogList.add(wmTaskLog5);
            }
            else if (wmTask.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PURCHASE)) {
                //4、工艺任务
                WmTaskLog wmTaskLog4 = new WmTaskLog();
                wmTaskLog4.setWmTaskId(wmTask.getId());
                wmTaskLog4.setContent("工艺任务");
                wmTaskLog4.setOperateBy(wmTask.getCreateBy());//默认值
                wmTaskLog4.setOperateTime(new Date());
                wmTaskLog4.setOperateType(UserConstant.TASK_LOG_TYPE_PROCESS);
                wmTaskLog4.setIsOver(UserConstant.SYS_NO);
                wmTaskLog4.setOrderNum(4);
                wmTaskLogList.add(wmTaskLog4);
                //5、采购物料
                WmTaskLog wmTaskLog5 = new WmTaskLog();
                wmTaskLog5.setWmTaskId(wmTask.getId());
                wmTaskLog5.setContent("采购物料");
                wmTaskLog5.setOperateType(UserConstant.TASK_LOG_TYPE_BOM_PURCHASE);
                wmTaskLog5.setIsOver(UserConstant.SYS_NO);
                wmTaskLog5.setOrderNum(5);
                wmTaskLogList.add(wmTaskLog5);
                //6、物料到货
                WmTaskLog wmTaskLog6 = new WmTaskLog();
                wmTaskLog6.setWmTaskId(wmTask.getId());
                wmTaskLog6.setContent("物料到货");
                wmTaskLog6.setOperateType(UserConstant.TASK_LOG_TYPE_BOM_REACHED);
                wmTaskLog6.setIsOver(UserConstant.SYS_NO);
                wmTaskLog6.setOrderNum(6);
                wmTaskLogList.add(wmTaskLog6);
            }
            else if (wmTask.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_DEBUG)) {
                //4、调试任务
                WmTaskLog wmTaskLog4 = new WmTaskLog();
                wmTaskLog4.setWmTaskId(wmTask.getId());
                wmTaskLog4.setContent("调试任务");
                wmTaskLog4.setOperateType(UserConstant.TASK_LOG_TYPE_DEBUG);
                wmTaskLog4.setIsOver(UserConstant.SYS_NO);
                wmTaskLog4.setOrderNum(4);
                wmTaskLogList.add(wmTaskLog4);
            }

            //99、任务完成
            WmTaskLog wmTaskLog99 = new WmTaskLog();
            wmTaskLog99.setWmTaskId(wmTask.getId());
            wmTaskLog99.setContent("任务完成");
            wmTaskLog99.setOperateType(UserConstant.TASK_LOG_TYPE_PLAN_FINISH);
            wmTaskLog99.setIsOver(UserConstant.SYS_NO);
            wmTaskLog99.setOrderNum(99);
            wmTaskLogList.add(wmTaskLog99);

            for (WmTaskLog wmTaskLog : wmTaskLogList) {
                save(wmTaskLog);
            }
            return wmTaskLogList;
        }
        else{
            return wmTaskLogListExist;
        }
    };

    @Override
    public void updateTaskLog(String wmTaskId, String operateType){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaUpdateWrapper<WmTaskLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WmTaskLog::getOperateBy, sysUser.getId())
                .set(WmTaskLog::getIsOver, UserConstant.SYS_YES)
                .set(WmTaskLog::getOperateTime, new Date())
                .set(WmTaskLog::getUpdateBy, sysUser.getId())
                .set(WmTaskLog::getUpdateTime, new Date())
                .eq(WmTaskLog::getWmTaskId, wmTaskId)
                .eq(WmTaskLog::getOperateType, operateType);
        update(null, updateWrapper);
    };

    @Override
    public List<WmTaskLog> queryByTaskId(String wmTaskId){
        LambdaQueryWrapper<WmTaskLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskLog::getWmTaskId, wmTaskId).orderByAsc(WmTaskLog::getOrderNum);
        List<WmTaskLog> wmTaskLogList = list(queryWrapper);
        return wmTaskLogList;
    };
}
